var app = getApp(), dataApi = require("../../provider/dataApi.js"), wxParse = (require("../../provider/pipe.js"), 
require("../../wxParse/wxParse.js"));

Page({
    data: {
        specialId: "",
        isAnalysisAudioPlay: 1,
        answerAudioList: [],
        answerImgList: [],
        isFirstPlay: !0,
        have: 1,
        type: 1,
        startPoint: [ 0, 0 ],
        curPoint: [],
        statusBarHeight: "",
        titleBarHeight: "",
        show: !1,
        num: 0,
        numLength: 0,
        remainder: 0,
        handPaperShow: !1,
        item: {
            totalScore: 100,
            passScore: 60,
            paperTime: 90,
            highest: 0
        },
        timer: null,
        ruleTime: "",
        durationTime: "",
        useTime: "",
        index: 0,
        paperDetail: "",
        answerList: [],
        audioList: [],
        answerLength: 0,
        isAudio: 1,
        inputHeight: 0,
        rightAnswer: [],
        isLoading: !1,
        adInfo: {},
        adHave: 2,
        userAnswerJudge: [],
        trialBool: !1,
        trialNum: "",
        isPageWait: !0,
        titlePlayTime: "00:00",
        titleTotalTime: "00:00",
        titleAudioTimer:
PlayTime: "00:00",
        analysisTotalTime: "00:00",
        analysisAudioTimer: null,
        audioTimeList: [],
        isShowShortAnswer: !1
    },
    previewImg: function(event) {
        var imageUrls = this.data.paperDetail[this.data.index].qimage;
        wx.previewImage({
            current: imageUrls[event.currentTarget.dataset.index],
            urls: imageUrls
        });
    },
    previewImgJx: function(event) {
        var imageUrls = this.data.paperDetail[this.data.index].aimage;
        wx.previewImage({
            current: imageUrls[event.currentTarget.dataset.index],
            urls: imageUrls
        });
    },
    myTouchStart: function(event) {
        this.setData({
            startPoint: [ event.touches[0].pageX, event.touches[0].pageY ]
        });
    },
    myTouchMove: function(event) {
        this.setData({
            curPoint: [ event.touches[0].pageX, event.touches[0].pageY ]
        });
    },
    myTouchEnd: function(event) {
        event.changedTouches[0].pageX - this.data.startPoint[0] < 0 ? this.data.index + 1 < this.data.paperDetail.length && this.setData({
            index: this.data.index + 1
        }) : 0 < this.data.index && this.setData({
            index: this.data.index - 1
        });
    },
    goType: function(event) {
        var type = event.currentTarget.dataset.type;
        var answerList = this.data.answerList, isAnalysis = !1;
        2 == type && (isAnalysis = !0), 
        // answerList[this.data.index].isanalysis = isAnalysis,
         this.setData({
            type: type,
            answerList: answerList
        });
    },
    onLoad: function(options) {
        if (wx.getStorageSync("uid")) {
            if (this.setData({
                statusBarHeight: app.globalData.statusBarHeight,
                titleBarHeight: app.globalData.titleBarHeight
            }), "{}" != JSON.stringify(options)) {
                console.log(options);
                var sonType = "";
                options.sonType && (sonType = options.sonType), this.setData({
                    trialNum: options.num,
                    trialBool: !0,
                    sonType: sonType
                });
            }
            var op = !0;
            "seeErr" == app.globalData.op && (op = !1), this.setData({
                isShowShortAnswer: op
            }), op = app.globalData.op, console.log(app.globalData.id), sonType = app.globalData.id, "sequence" == op || "randoms" == op || "notdone" == op || "qtype" == op ? this.mockExam(op, sonType, app.globalData.lib_id) : this.mockExam(op, sonType, ""), 
            this.queryAd();
        } else wx.navigateTo({
            url: "/pages/login/login"
        });
    },
    queryAd: function() {
        var self = this;
        dataApi.advert({
            op: "second"
        }).then(function(response) {
            self.setData({
                adInfo: response.data.info,
                adHave: response.data.ad_have
            });
        }).catch(function(error) {
            console.log(error);
        });
    },
    goBack: function() {
        2 != this.data.have && this.changepic(this.data.index), wx.navigateBack({
            delta: 1
        });
    },
    showTk: function() {
        if (100 < this.data.paperDetail) {
            var num = Math.floor(this.data.paperDetail.length / 100), remainder = this.data.paperDetail.length % 100;
            console.log(num, remainder), this.setData({
                num: 100,
                numLength: num,
                remainder: remainder
            });
        } else this.setData({
            num: this.data.paperDetail
        });
        this.setData({
            show: !0
        });
    },
    onClose: function() {
        this.setData({
            show: !1
        });
    },
    goUp: function() {
        if (0 == this.data.index) return !1;
        this.setData({
            index: this.data.index - 1
        });
        var updateData = {};
        this.setData((updateData["answerList[" + (this.data.index + 1) + "].isanalysis"] = !1, updateData.isPageWait = !0, 
        updateData)), this.judgeShortAnswer(), this.changepic(this.data.index), this.initUserAnswer(this.data.index), 
        this.wxParseTitle(this.data.index), this.clearAudioTime();
    },
    goDown: function() {
        if (this.data.trialBool) {
            var trialNum = this.data.trialNum;
            if (this.data.index + 1 >= trialNum) return wx.showToast({
                title: "本套试题只能试答" + trialNum + "题哦~",
                icon: "none"
            }), !1;
        }
        if (this.data.index + 1 >= this.data.paperDetail.length) return wx.showToast({
            title: "已做完所有习题",
            icon: "none"
        }), !1;
        this.setData({
            index: this.data.index + 1,
            isPageWait: !0
        }), updateData = {}, this.setData((updateData["answerList[" + (this.data.index - 1) + "].isanalysis"] = !1, 
        updateData)), this.judgeShortAnswer(), this.changepic(this.data.index), this.initUserAnswer(this.data.index), 
        this.wxParseTitle(this.data.index), this.clearAudioTime();
		
		if(this.data.type == 2 ){
            this.lookAnaiysis();
        }
    },
    jumptap: function(event) {
        if (this.data.trialBool) {
            var index = event.currentTarget.dataset.i;
            if (console.log(index), index >= this.data.trialNum) return wx.showToast({
                title: "此为试答，如想答剩下的题请先去购买此套试卷哦",
                icon: "none"
            }), !1;
        }
        this.setData({
            index: event.currentTarget.dataset.i,
            show: !1,
            isPageWait: !0
        }), this.judgeShortAnswer(), this.changepic(this.data.index), this.initUserAnswer(this.data.index), 
        this.wxParseTitle(this.data.index), this.clearAudioTime();
    },
    judgeShortAnswer: function() {
        var isShowShortAnswer = this.data.isShowShortAnswer;
        8 == this.data.paperDetail[this.data.index].type && (isShowShortAnswer ? this.setData({
            type: 1
        }) : this.setData({
            type: 2
        }));
    },
    initUserAnswer: function(index) {
        if (4 == (paperDetail = this.data.paperDetail)[index].type) {
            index = paperDetail[index].rightarray;
            for (var userAnswerJudge = [], i = 0; i < index.length; i++) userAnswerJudge.push(!0);
            this.setData({
                userAnswerJudge: userAnswerJudge
            });
        }
    },
    scrolltolower: function(event) {
        this.data.num < this.data.paperDetail.length && (Math.floor(this.data.num / 100) == this.data.numLength ? this.setData({
            num: this.data.num + this.data.remainder
        }) : this.setData({
            num: this.data.num + 100
        }));
    },
    addWrong: function() {
        var self = this;
        dataApi.addWrong({
            uid: wx.getStorageSync("uid"),
            tid: this.data.paperDetail[this.data.index].id,
            test_type: this.data.paperDetail[this.data.index].type,
            wrong_have: this.data.paperDetail[this.data.index].wrong_have
        }).then(function(response) {
            console.log(response);
            var wrongHaveKey = "paperDetail[" + self.data.index + "].wrong_have";
            1 == response.data ? (wx.showToast({
                title: "已加入收藏",
                icon: "none"
            }), response = {}, self.setData((response[wrongHaveKey] = 1, response))) : (wx.showToast({
                title: "已移出收藏",
                icon: "none"
            }), response = {}, self.setData((response[wrongHaveKey] = 2, response)));
        }).catch(function(error) {
            console.log(error);
        });
    },
    choice: function(event) {
        var answerList = this.data.answerList, correctCount = 0;
        answerList[event.currentTarget.dataset.fjtit].choose = event.currentTarget.dataset.option, answerList.forEach(function(item) {
            2 == item.type ? item.flag && correctCount++ : 4 == item.type ? item.issure && correctCount++ : 5 == item.type ? item.iscomplete && correctCount++ : item.choose && correctCount++;
        }), this.setData({
            answerList: answerList,
            answerLength: correctCount
        }), this.judge(this.data.index), this.recordAnswer();
    },
    multipleChoice: function(event) {
        var fjTit = event.currentTarget.dataset.fjtit, option = event.currentTarget.dataset.option, isChoose = 0, correctCount = 0;
        (answerList = this.data.answerList)[fjTit].choose.forEach(function(item, index) {
            item.o == option && (item.ischoose = !item.ischoose);
        });
        for (var i = 0; i < answerList[fjTit].choose.length; i++) {
            if (answerList[fjTit].choose[i].ischoose) {
                isChoose = 1, answerList[fjTit].flag = !1;
                break;
            }
            isChoose = 0, answerList[fjTit].flag = !1;
        }
        answerList.forEach(function(item) {
            2 == item.type ? item.flag && correctCount++ : 4 == item.type ? item.issure && correctCount++ : 5 == item.type ? item.iscomplete && correctCount++ : item.choose && correctCount++;
        }), answerList[fjTit].flag = 1 == isChoose, this.setData({
            answerLength: correctCount + isChoose,
            answerList: answerList
        });
    },
    gomultipleChoice: function(event) {
        var fjTit = event.currentTarget.dataset.fjtit, answerList = this.data.answerList, item = event.currentTarget.dataset.item, selectedOptions = [];
        answerList[fjTit].choose.forEach(function(item) {
            item.ischoose && selectedOptions.push(item.o);
        }), (selectedOptions = selectedOptions.join(",")) == item.rightkey && (answerList[fjTit].isright = !0), 0 < selectedOptions.length ? (answerList[fjTit].issure = !0, 
        item.rightkey = item.rightkey.split(","), answerList[fjTit].choose.forEach(function(item) {
            item.ischoose ? -1 != item.rightkey.indexOf(item.o) ? item.ischooseright = 1 : -1 == item.rightkey.indexOf(item.o) && (item.ischooseright = 2) : -1 != item.rightkey.indexOf(item.o) ? item.ischooseright = 3 : -1 == item.rightkey.indexOf(item.o) && (item.ischooseright = 4);
        }), this.setData({
            answerList: answerList
        }), this.judge(this.data.index), this.recordAnswer()) : wx.showToast({
            icon: "none",
            title: "请选择你的答案~"
        });
    },
    textInput: function(event) {
        var fjNum = event.currentTarget.dataset.fjnum, answerList = this.data.answerList;
        answerList[fjNum].choose[event.currentTarget.dataset.inputnum] = event.detail.value;
        var correctCount = 0;
        this.isempty(answerList[fjNum].choose) ? answerList[fjNum].issure = !0 : answerList[fjNum].issure = !1, answerList.forEach(function(item) {
            2 == item.type ? item.flag && correctCount++ : 4 == item.type ? item.issure && correctCount++ : 5 == item.type ? item.iscomplete && correctCount++ : item.choose && correctCount++;
        }), this.setData({
            answerList: answerList,
            answerLength: correctCount
        });
    },
    goSure: function(event) {
        var fjNum = event.currentTarget.dataset.fjnum;
        var answerList = this.data.answerList;
        console.log(fjNum), console.log(this.data.index), console.log(answerList), answerList[fjNum] && this.isempty(answerList[fjNum].choose) ? (answerList[fjNum].iswrite = !0, 
        this.setData({
            answerList: answerList
        }), this.judge(this.data.index), this.recordAnswer()) : wx.showToast({
            icon: "none",
            title: "请填写完整~"
        });
    },
    judgeCompletionItem: function() {
        for (var index = this.data.index, rightKey = this.data.paperDetail[index].rightkey.split("|"), userAnswer = this.data.answerList[index].choose, judgeResult = (index = [], 
        rightKey = rightKey.map(function(item) {
            return item.replace(/\u3011\u3010/g, "】,【");
        }).map(function(item) {
            return item.split(",");
        }), userAnswer = userAnswer.map(function(item) {
            return "【" + item + "】";
        }), 0); judgeResult < userAnswer.length; judgeResult++) {
            for (var isCorrect = !1, keyIndex = 0; keyIndex < rightKey.length; keyIndex++) userAnswer[judgeResult] == rightKey[keyIndex][judgeResult] && (isCorrect = !0);
            index.push(isCorrect);
        }
        this.setData({
            userAnswerJudge: index
        });
    },
    vioceChoice: function(event) {
        console.log(event);
        var fjNum = event.currentTarget.dataset.fjnum, answerList = this.data.answerList, correctCount = 0, updateData = {};
        this.setData((updateData["answerList[" + fjNum + "].choose[" + event.currentTarget.dataset.zjnum + "].choose"] = event.currentTarget.dataset.option, 
        updateData)), this.isempty(answerList[fjNum].choose) ? answerList[fjNum].iscomplete = !0 : answerList[fjNum].iscomplete = !1, answerList.forEach(function(item) {
            2 == item.type ? item.flag && correctCount++ : 4 == item.type ? item.issure && correctCount++ : 5 == item.type ? item.iscomplete && correctCount++ : item.choose && correctCount++;
        }), console.log(answerList), this.setData({
            answerList: answerList,
            answerLength: correctCount
        }), this.isBigjudge(fjNum), this.recordAnswer();
    },
    chooseOption: function(event) {
        var index = this.data.index, answerList = this.data.answerList;
        answerList[index].choose[event.currentTarget.dataset.findex].userAnswer = event.currentTarget.dataset.zindex, 
        console.log(answerList[index].choose), this.setData({
            answerList: answerList
        }), this.isBigjudge(index), this.recordAnswer();
    },
    isBigjudge: function(index) {
        var answerList = this.data.answerList, item = answerList[index], choiceLength = item.choose.length, isCorrect = !0;
        if (5 == item.type) {
            for (var i = 0; i < choiceLength; i++) if ("" == item.choose[i].choose) return;
            for (i = 0; i < choiceLength; i++) item.choose[i].isright != item.choose[i].choose && (isCorrect = !1);
        }
        if (6 == answerList[index].type || 7 == answerList[index].type) {
            for (i = 0; i < choiceLength; i++) if ("" == item.choose[i].userAnswer) return;
            for (i = 0; i < choiceLength; i++) item.choose[i].isright != item.choose[i].userAnswer && (isCorrect = !1);
        }
        answerList[index].isjudge = isCorrect ? 1 : 2, this.setData({
            answerList: answerList
        });
    },
    bindShortAnswerBlur: function(event) {
        var answerList = this.data.answerList;
        answerList[this.data.index].choose = event.detail.value, this.setData({
            answerList: answerList
        });
    },
    shortAnswerSure: function(event) {
        console.log(this.data.answerList), event = this.data.answerList;
        var index = this.data.index;
        "" == event[index].choose ? wx.showToast({
            icon: "none",
            title: "请先输入您的答案"
        }) : (event[index].isanalysis = false, event[index].issure = !0, this.setData({
            answerList: event
        }), this.pageScrollTo("#analysis"), this.lookAnaiysis());
    },
    pageScrollTo: function(selector) {
        var query = wx.createSelectorQuery();
        query.selectViewport().scrollOffset(), query.select(selector).boundingClientRect(), query.exec(function(results) {
            console.log(results), wx.pageScrollTo({
                scrollTop: results[0].scrollTop + results[1].top - 500,
                duration: 300
            });
        });
    },
    onReady: function() {
        var audioContext = wx.createInnerAudioContext();
        this.setData({
            audioCtx: audioContext
        });
    },
    onHide: function() {
        this.changepic(this.data.index), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
    },
    onUnload: function() {
        this.changepic(this.data.index), wx.createVideoContext([ "index", this.data.playIndex ].join("")).pause();
        var titleAudioTimer = clearInterval(this.data.titleAudioTimer), analysisAudioTimer = clearInterval(this.data.analysisAudioTimer);
        this.setData({
            titleAudioTimer: titleAudioTimer,
            analysisAudioTimer: analysisAudioTimer
        });
    },
    goCorrection: function(event) {
        app.globalData.id = event.currentTarget.id, wx.navigateTo({
            url: "../feedback/feedback"
        });
    },
    bindplay: function(t) {
        if (console.log(t), t = t.currentTarget.id, this.data.playIndex) {
            var a = wx.createVideoContext([ "index", this.data.playIndex ].join(""));
            a.seek(0), a.pause(), console.log([ "index", this.data.playIndex ].join("")), this.setData({
                playIndex: t
            }), console.log([ "index", this.data.playIndex ].join("")), wx.createVideoContext([ "index", this.data.playIndex ].join("")).play();
        } else this.setData({
            playIndex: t
        }), wx.createVideoContext([ "index", t ].join("")).play();
    },
    mockExam: function(e, i, s) {
        var n = this;
        this.setData({
            isloading: !0
        });
        var o = "";
        t.globalData.paperId && (o = t.globalData.paperId), console.log(e), a.sequence({
            uid: wx.getStorageSync("uid"),
            typeId: i,
            son_type: this.data.sonType,
            op: e,
            paperid: o,
            lib_id: s
        }).then(function(t) {
            if (console.log(t), t.data.list.length <= 0) n.setData({
                have: t.data.have,
                isloading: !1
            }); else {

   
              
              let list = t.data.list;

              for (let index = 0; index < list.length; index++) {
                const qdetail = list[index];

                if( 1 == qdetail.type || 2==qdetail.type ) {

                let options = qdetail.option;
                let newOption = [];
                for(let i=0;i<options.length;i++) {
                  if(options[i].o=="A"){
                    newOption[0] = options[i]
                  }
                  if(options[i].o=="B"){
                    newOption[1] = options[i]
                  } 
                  if(options[i].o=="C"){
                    newOption[2] = options[i]
                  }    
                   if(options[i].o=="D"){
                    newOption[3] = options[i]
                  }    
                   if(options[i].o=="E"){
                    newOption[4] = options[i]
                  }     
                  if(options[i].o=="F"){
                    newOption[5] = options[i]
                  }
                }
        
                qdetail.option = newOption;
              }
            }


                var a = [], e = [], i = [], s = [], o = [], r = [];
                t.data.list && (t.data.list.forEach(function(t) {
                    if (1 == t.type || 3 == t.type || 8 == t.type) a.push({
                        type: t.type,
                        tid: t.id,
                        issure: !1,
                        choose: "",
                        isanalysis: !1,
                        isjudge: 3
                    }); else if (2 == t.type) {
                        var n = t.rightkey.split(","), d = [];
                        t.option.forEach(function(t) {
                            d.push({
                                o: t.o,
                                ischoose: !1
                            });
                        }), a.push({
                            type: t.type,
                            tid: t.id,
                            issure: !1,
                            flag: !1,
                            choose: d,
                            isanalysis: !1,
                            isjudge: 3
                        }), t.option.forEach(function(t) {
                            -1 != n.indexOf(t.o) ? t.right = !0 : t.right = !1;
                        });
                    } else if (4 == t.type) {
                        for (var h = [], l = 0; l < t.rightarray.length; l++) h.push("");
                        a.push({
                            type: t.type,
                            tid: t.id,
                            issure: !1,
                            choose: h,
                            iswrite: !1,
                            isanalysis: !1,
                            isjudge: 3
                        });
                    } else if (5 == t.type) {
                        var u = [], c = [];
                        t.list.forEach(function(t) {
                            u.push({
                                tid: t.id,
                                isright: t.rightkey,
                                choose: ""
                            }), c.push(t.rightkey);
                        }), t.rightkey = c.join(","), a.push({
                            type: t.type,
                            tid: t.id,
                            iscomplete: !1,
                            choose: u,
                            isanalysis: !1,
                            isjudge: 3
                        });
                    } else if (6 == t.type || 7 == t.type) {
                        var p = [];
                        t.list instanceof Array && 0 < t.list.length && (h = t.list.map(function(t, a) {
                            return p.push(t.rightkey), {
                                tid: t.id,
                                isright: t.rightkey,
                                userAnswer: ""
                            };
                        }), t.rightkey = p.join(","), a.push({
                            type: t.type,
                            tid: t.id,
                            issure: !1,
                            choose: h,
                            isanalysis: !1,
                            isDo: !1,
                            isjudge: 3
                        }));
                    } else 8 == t.type && "" == t.uanswer && (t.uanswer = "暂未作答");
                    t.qaudio ? e.push({
                        audiosrc: t.qaudio,
                        time: 0
                    }) : e.push({
                        audiosrc: "",
                        time: 0
                    }), 2 == t.a_type ? i.push(t.option) : i.push(""), 4 == t.type ? s.push(t.rightarray.join(",")) : s.push(t.rightkey), 
                    t.analysis_audio ? o.push({
                        audiosrc: t.analysis_audio,
                        time: 0
                    }) : o.push({
                        audiosrc: "",
                        time: 0
                    }), r.push(t.aimage);
                }), console.log(t.data.list), t.data.list && n.setData({
                    paperdetail: t.data.list,
                    answerlist: a,
                    audiolist: e,
                    rightAnswer: s,
                    answerAudiolist: o,
                    answerImglist: r
                }), t.data.last_id && n.setData({
                    index: Number(t.data.last_id)
                }), n.setData({
                    isloading: !1
                }), n.getAllAudioTime(), n.judgeShortAnswer(), n.wxParseTitle(n.data.index));
            }
        }).catch(function(t) {
            n.setData({
                isloading: !1
            }), console.log(t);
        });
    },
    getAllAudioTime: function() {
        var t = [];
        this.data.paperdetail.forEach(function() {
            t.push({
                title: "00:00",
                analysis: "00:00"
            });
        }), this.setData({
            audioTimeList: t
        });
    },
    wxParseTitle: function(questionIndex) {
        var questionDetail = this.data.paperDetail[questionIndex];
       
        wxParse.wxParse("ParseTitle", "html", questionDetail.question, this, 5), 
        console.log(this.data.ParseTitle), 0 != questionDetail.a_type || 1 != questionDetail.type && 2 != questionDetail.type ? this.setData({
            isPageWait: !1
        }) : this.wxParseOptions(questionDetail.option);
    },
    wxParseOptions: function(options) {
        debugger;
        for (var optionIndex = 0; optionIndex < options.length; optionIndex++) {
            wxParse.wxParse("wxParseOption" + optionIndex, "html", options[optionIndex].p, this);
        }
        this.setData({
            isPageWait: !1
        });
    },
    isEmpty: function(input) {
        var isEmpty = !0;
        for (var key in input) input[key] || (isEmpty = !1);
        return !!isEmpty;
    },
    lookAnalysis: function() {
        let that = this;
        var answerList = this.data.answerList, currentIndex = this.data.index;
        var currentAnswer = answerList[currentIndex].choose;
        debugger;
        if (!currentAnswer && that.data.type == 1) {
            wx.showToast({
                title: "请填写答案后再查看解析",
                icon: "none"
            });
            return;
        }

        if (!answerList[currentIndex].isAnalysis) {
            answerList[currentIndex].isAnalysis = !0;
            this.setData({
                answerList: answerList
            });
            console.log(this.data.paperDetail[currentIndex].analysis);
            wxParse.wxParse("article", "html", this.data.paperDetail[currentIndex].analysis, this, 15);
        }
    },
    changePic: function(questionIndex) {
        if (2 != this.data.have && 0 != this.data.audioList.length && (!this.data.audioList[questionIndex].audiosrc || this.data.audioList[questionIndex].audiosrc != this.data.audioList[questionIndex - 1].audiosrc && 0 < questionIndex)) {
            this.setData({
                isAudio: 1,
                isAnalysisAudioPlay: 1
            });
            this.data.audioCtx.pause();
        }
    },
    myAudioStart: function(event) {
        var audioContext = this.data.audioCtx, that = this, audioType = event.currentTarget.dataset.type;
        if (console.log("播放解析=" + audioType), "paper" == audioType) {
            var analysisAudioTimer = clearInterval(this.data.analysisAudioTimer);
            this.setData({
                isAudio: 2,
                isAnalysisAudioPlay: 1,
                titlePlayTime: "00:00",
                analysisAudioTimer: analysisAudioTimer
            });
            audioContext.src = event.currentTarget.dataset.src;
            audioContext.play();
            this.currenttime();
            if (!this.data.onEnded) {
                this.data.onEnded = !0;
                audioContext.onEnded(function() {
                    that.setData({
                        isAudio: 1,
                        currentTime: 0
                    });
                    that.audioTime(event.currentTarget.dataset.src, 0);
                });
            }
            var audioIndex = event.currentTarget.dataset.num, audioTimeList = that.data.audioTimeList, titleAudioTimer = setInterval(function() {
                var currentTime = Math.round(audioContext.currentTime), durationTime = Math.round(audioContext.duration), formattedCurrentTime = that.handleTime(currentTime), formattedDurationTime = that.handleTime(durationTime);
                audioTimeList[audioIndex].title = formattedDurationTime;
                that.setData({
                    titlePlayTime: formattedCurrentTime,
                    titleTotalTime: formattedDurationTime,
                    audioTimeList: audioTimeList,
                    titleAudioTimer: titleAudioTimer
                });
                if (durationTime == currentTime) {
                    console.log("停止");
                    setTimeout(function() {
                        clearInterval(titleAudioTimer);
                        that.setData({
                            titlePlayTime: "00:00",
                            titleTotalTime: formattedDurationTime,
                            titleAudioTimer: titleAudioTimer
                        });
                    }, 1000);
                }
            }, 1000);
        }
        if ("analysis" == audioType) {
            var titleAudioTimer = clearInterval(this.data.titleAudioTimer);
            this.setData({
                isAnalysisAudioPlay: 2,
                isAudio: 1,
                titlePlayTime: "00:00",
                titleAudioTimer: titleAudioTimer
            });
            audioContext.src = event.currentTarget.dataset.src;
            audioContext.play();
            this.currenttime();
            if (!this.data.onEnded) {
                this.data.onEnded = !0;
                audioContext.onEnded(function() {
                    that.setData({
                        isAnalysisAudioPlay: 1,
                        currentTime: 0
                    });
                    that.audioTime(event.currentTarget.dataset.src, 0);
                });
            }
            var analysisAudioIndex = event.currentTarget.dataset.num, audioTimeList = that.data.audioTimeList, analysisAudioTimer = setInterval(function() {
                var currentTime = Math.round(audioContext.currentTime), durationTime = Math.round(audioContext.duration), formattedCurrentTime = that.handleTime(currentTime), formattedDurationTime = that.handleTime(durationTime);
                audioTimeList[analysisAudioIndex].analysis = formattedDurationTime;
                that.setData({
                    analysisPlayTime: formattedCurrentTime,
                    analysisTotalTime: formattedDurationTime,
                    audioTimeList: audioTimeList,
                    analysisAudioTimer: analysisAudioTimer
                });
                if (durationTime == currentTime) {
                    console.log("停止");
                    setTimeout(function() {
                        clearInterval(analysisAudioTimer);
                        that.setData({
                            analysisPlayTime: "00:00",
                            analysisTotalTime: formattedDurationTime,
                            analysisAudioTimer: analysisAudioTimer
                        });
                    }, 1000);
                }
            }, 1000);
        }
    },
    myAudioPause: function(event) {
        var audioContext = this.data.audioCtx, that = this;
        audioContext.pause();
        var audioType = event.currentTarget.dataset.type;
        if (console.log("暂停解析=" + audioType), "paper" == audioType) {
            audioContext.onPause(function() {
                audioContext.currentTime;
                that.setData({
                    isAudio: 1
                });
                that.audioTime(event.currentTarget.dataset.src, Math.floor(audioContext.currentTime));
            });
            var titleAudioTimer = clearInterval(that.data.titleAudioTimer);
            that.setData({
                titleAudioTimer: titleAudioTimer
            });
        }
        "analysis" == audioType && audioContext.onPause(function() {
            audioContext.currentTime;
            that.setData({
                isAnalysisAudioPlay: 1
            });
            that.audioTime(event.currentTarget.dataset.src, Math.floor(audioContext.currentTime));
        });
    },
    handleTime: function(seconds) {
        if (60 <= seconds) {
            var minutes = parseInt(seconds / 60 % 60);
            return (minutes < 10 ? "0" + minutes : minutes) + ":" + ((seconds = parseInt(seconds % 60)) < 10 ? "0" + seconds : seconds);
        }
        return "00:" + (seconds < 10 ? "0" + seconds : seconds);
    },
    clearAudioTime: function() {
        var questionIndex = this.data.index, audioTimeList = this.data.audioTimeList, titleTime = audioTimeList[questionIndex].title;
        var analysisTime = audioTimeList[questionIndex].analysis;
        clearInterval(this.data.titleAudioTimer);
        clearInterval(this.data.analysisAudioTimer);
        this.setData({
            titlePlayTime: "00:00",
            titleTotalTime: titleTime,
            analysisPlayTime: "00:00",
            analysisTotalTime: analysisTime
        });
    },
    currenttime: function() {
        var audioContext = this.data.audioCtx;
        setTimeout(function() {
            audioContext.currentTime;
            audioContext.onTimeUpdate(function() {});
        }, 100);
    },
    audioTime: function(audioSrc, currentTime) {
        this.data.audioList.forEach(function(audioItem) {
            audioItem.audiosrc == audioSrc && (audioItem.time = currentTime);
        });
    },
    judge: function(questionIndex) {
        var answerList = this.data.answerList, rightAnswer = this.data.rightAnswer, userAnswer = "";
        if (2 == answerList[questionIndex].type) {
            var selectedOptions = [];
            answerList[questionIndex].choose.forEach(function(option) {
                option.ischoose && selectedOptions.push(option.o);
            });
            userAnswer = selectedOptions.join(",");
        } else if (4 == answerList[questionIndex].type) {
            userAnswer = answerList[questionIndex].choose.join(",");
        } else if (1 != answerList[questionIndex].type && 3 != answerList[questionIndex].type) {
            userAnswer = answerList[questionIndex].choose;
        }
        userAnswer && (answerList[questionIndex].isjudge = userAnswer == rightAnswer[questionIndex] ? 1 : 2);
        this.setData({
            answerList: answerList
        });
    },
    recordAnswer: function() {
        var that = this, answerList = this.data.answerList, currentIndex = this.data.index, answers = [], operationType = "";
        answerList.forEach(function(answerItem) {
            if (1 == answerItem.type || 3 == answerItem.type || 3 == answerItem.type) answers.push({
                tid: answerItem.tid,
                type: answerItem.type,
                answer: answerItem.choose
            }); else if (2 == answerItem.type) {
                var selectedOptions = [];
                answerItem.choose.forEach(function(option) {
                    option.ischoose && selectedOptions.push(option.o);
                });
                answers.push({
                    tid: answerItem.tid,
                    type: answerItem.type,
                    answer: selectedOptions
                });
            } else if (4 == answerItem.type) {
                var formattedAnswers = [];
                answerItem.choose.forEach(function(answer) {
                    formattedAnswers.push("【" + answer + "】");
                });
                formattedAnswers = formattedAnswers.join(",");
                answers.push({
                    tid: answerItem.tid,
                    type: answerItem.type,
                    answer: formattedAnswers
                });
            } else if (5 == answerItem.type) {
                var completionAnswers = [];
                answerItem.choose.forEach(function(answer) {
                    completionAnswers.push({
                        tid: answer.tid,
                        answer: answer.choose
                    });
                });
                answers.push({
                    tid: answerItem.tid,
                    type: answerItem.type,
                    answer: completionAnswers
                });
            } else if (6 == answerItem.type || 7 == answerItem.type) {
                var userAnswers = [];
                answerItem.choose.forEach(function(answer) {
                    userAnswers.push({
                        tid: answer.tid,
                        answer: answer.userAnswer
                    });
                });
                answers.push({
                    tid: answerItem.tid,
                    type: answerItem.type,
                    answer: userAnswers
                });
            }
        });
        var specialId = "", typeId = "";
        "sequence" == app.globalData.op ? operationType = "shunxu" : "qhigh" == app.globalData.op ? operationType = "qhigh" : "intensive" == app.globalData.op ? operationType = "intensive" : "special" == app.globalData.op || "qspecial" == app.globalData.op ? (operationType = "qspecial", 
        specialId = this.data.specialId) : "qtype" == app.globalData.op || "qtype_submit" == app.globalData.op ? (operationType = "qtype_submit", 
        typeId = this.data.specialId) : operationType = "other";
        dataApi.sequence_submit({
            uid: wx.getStorageSync("uid"),
            answerdata: JSON.stringify(answers[currentIndex]),
            op: operationType,
            index: currentIndex,
            special_id: specialId,
            type_id: typeId,
            lib_id: app.globalData.lib_id
        }).then(function(response) {
            console.log(response);
            4 == answerList[currentIndex].type && that.judgeCompletionItem();
        }).catch(function(error) {
            console.log(error);
        });
    },
    onShareAppMessage: function() {
        return {
            path: "/pages/home/home"
        };
    }
});